var temptext = '';
 	var lancn='en';
		var lanto='es';
		b1.disabled=false;
	  
      function nl2br(text){
	  var re_nlchar='';
      var t=text;
      text = escape(text);
  
      if(text.indexOf('%0D%0A') > -1){
   
      re_nlchar = /%0D%0A/g ;
  
      }else if(text.indexOf('%0A') > -1){
  
      re_nlchar = /%0A/g ;
   
      }else if(text.indexOf('%0D') > -1){
        re_nlchar = /%0D/g ;

      }
	  if(re_nlchar=='')
	  {
	  return t;
	  }
	  else
	  {
 
      return unescape( text.replace(re_nlchar,'<br />') );
	  }
 
      }

function t()
{b1.disabled=true;
var container2 = document.getElementById("result2");
container2.innerHTML='Start rewriting the article, please wait...';
var sourceText = $('#source').val();

  google.language.detect(sourceText.substring(0,500), function(result) {
 
    // If there wasn't an error in the request
    if (!result.error) {
      lancn = result.language;
    
      }
	  });
	  if (lancn!='en') 
	  {lanto='en';}
sourceText = nl2br(sourceText);
$.translate(sourceText,lancn, lanto, {returnAll:true, limit:1000,
                 start:         function(){   }, 
                 complete:   function(translation)
                 {temptext= translation;ret();},
                 error:         function()
                 { container2.innerHTML='Server busy, please try again later';  
                 b1.disabled=false;  } });}
function ret()
{var container2 = document.getElementById("result2");
if(temptext!='')
{$.translate(temptext, lanto,  lancn, {returnAll:true, limit:1000, 
                     start:         function(){   }, 
                     complete:  function(translation)
                     { container2.innerHTML=translation;
                     b1.disabled=false;
                     },
                     error:         function(){  container2.innerHTML='Server busy, please try again later';                                                               b1.disabled=false; 
                                                                 }
							  });


}
}


 function s(wsel,se){ var frl=document.getElementById(wsel); if (frl.length>0){ return;}var a=google.language.Languages;var r=0; for(var key in a){ var lan= key.toUpperCase(); var lcode=a[key];if (google.language.isTranslatable(lcode)) { if(lcode==se)frl.options[r]= new Option (lan,lcode,true,true);else frl.options[r]=new Option (lan,lcode,false,false);r++;}}}
